/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui;

import java.awt.Dimension;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.model.PerformanceSequence;

/**
 *
 * @author Jannes Plyson
 */
public class TimeTextField extends JFormattedTextField{
    private NumberFormat format2Digits;
    private NumberFormat format3Digits;
    private MutableValueNode<Long> node;    
    private PerformanceSequence perSeq;    
    public TimeTextField(long time){
        super(getMaskFormatter());                        
        format2Digits = NumberFormat.getIntegerInstance();
        format2Digits.setMinimumIntegerDigits(2);
        format3Digits = NumberFormat.getIntegerInstance();
        format3Digits.setMinimumIntegerDigits(3);
        this.setMaximumSize(new Dimension(150,20));
        setTime(time);
    }

    public static MaskFormatter getMaskFormatter(){
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter("##:##:##.###.###.###"); //hours:minutes:seconds:mili:micro:nano
            formatter.setPlaceholderCharacter('0');
        } catch (ParseException ex) {}
        return formatter;
    }

    public void setTime(long nanoTime){
       this.setText(getTimeString(nanoTime));
    }

    public static String getTimeString(long nanoTime){
        long secondTime = nanoTime/1000000000l;
        int hours   = (int) ((secondTime) / 3600);
        int minutes = (int) (((secondTime) % 3600)/60);
        int seconds = (int) ((secondTime) % 60);
        long commaTime = nanoTime % 1000000000l;
        int mili = (int)(commaTime /1000000l);
        int micro = (int)((commaTime/1000)%1000);
        int nano = (int)(commaTime % 1000);
        NumberFormat format2Dig = NumberFormat.getIntegerInstance();
        format2Dig.setMinimumIntegerDigits(2);
        NumberFormat format3Dig = NumberFormat.getIntegerInstance();
        format3Dig.setMinimumIntegerDigits(3);
        return ("" + format2Dig.format(hours) + ":" + format2Dig.format(minutes)
                + ":" + format2Dig.format(seconds) + "." + format3Dig.format(mili)
                + "." + format3Dig.format(micro) + "." + format3Dig.format(nano));
    }
    
    public long getNanoTime(){
        String text = getText();
        String[] values = text.split("[:.]");
        try{
            long hours = Long.parseLong(values[0]);
            long minutes = Long.parseLong(values[1]);
            long seconds = Long.parseLong(values[2]);
            long mili = Long.parseLong(values[3]);
            long micro = Long.parseLong(values[4]);
            long nano = Long.parseLong(values[5]);
            long value = nano+micro*1000l+mili*1000000l+seconds*1000000000l
                    + minutes* 60000000000l + hours* 3600000000000l;
            return value;
        }catch(Exception exc){}
        return 0l;
    }      
}
